package com.ktg.mes.wm.service.impl;

import java.io.Serializable;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ktg.common.constant.UserConstants;
import com.ktg.common.utils.DateUtils;
import com.ktg.common.utils.SecurityUtils;
import com.ktg.common.utils.StringUtils;
import com.ktg.mes.unit.other.ZLServiceIml;
import com.ktg.common.core.domain.model.SqlResult;
import com.ktg.mes.wm.domain.tx.IssueTxBean;
import org.springframework.stereotype.Service;
import com.ktg.mes.wm.mapper.WmIssueHeaderMapper;
import com.ktg.mes.wm.domain.WmIssueHeader;
import com.ktg.mes.wm.service.IWmIssueHeaderService;

/**
 * 生产领料单头Service业务层处理
 *
 * @author yinjinlu
 * @date 2022-07-14
 */
@Service
public class WmIssueHeaderServiceImpl  extends ZLServiceIml<WmIssueHeaderMapper,WmIssueHeader> implements IWmIssueHeaderService
{

    @Override
    public WmIssueHeader findOld(WmIssueHeader model) {
        return getById(model.getIssueId());
    }

    /**
     * 查询生产领料单头
     *
     * @param issueId 生产领料单头主键
     * @return 生产领料单头
     */
    @Override
    public WmIssueHeader selectWmIssueHeaderByIssueId(Long issueId)
    {

        return getById(issueId);
    }

    /**
     * 查询生产领料单头列表
     *
     * @param wmIssueHeader 生产领料单头
     * @return 生产领料单头
     */
    @Override
    public List<WmIssueHeader> selectWmIssueHeaderList(WmIssueHeader wmIssueHeader)
    {
        QueryWrapper<WmIssueHeader> queryWrapper = new QueryWrapper<>(wmIssueHeader);
        return list(queryWrapper);
    }

    @Override
    public String checkIssueCodeUnique(WmIssueHeader wmIssueHeader) {
        WmIssueHeader header = getBaseMapper().checkIssueCodeUnique(wmIssueHeader);
        Long headerId = wmIssueHeader.getIssueId()==null?-1l:wmIssueHeader.getIssueId();
        if(StringUtils.isNotNull(header) && headerId.longValue() !=header.getIssueId().longValue()){
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 新增生产领料单头
     *
     * @param wmIssueHeader 生产领料单头
     * @return 结果
     */
    @Override
    public SqlResult insertWmIssueHeader(WmIssueHeader wmIssueHeader)
    {
        wmIssueHeader.setUpdateBy(SecurityUtils.getUsername());
        wmIssueHeader.setCreateTime(DateUtils.getNowDate());
        return insertSQLModel(wmIssueHeader);
    }

    /**
     * 修改生产领料单头
     *
     * @param wmIssueHeader 生产领料单头
     * @return 结果
     */
    @Override
    public SqlResult updateWmIssueHeader(WmIssueHeader wmIssueHeader)
    {
        wmIssueHeader.setUpdateBy(SecurityUtils.getUsername());
        wmIssueHeader.setUpdateTime(DateUtils.getNowDate());
        return updateSQLModel(wmIssueHeader);
    }

    /**
     * 批量删除生产领料单头
     *
     * @param issueIds 需要删除的生产领料单头主键
     * @return 结果
     */
    @Override
    public SqlResult deleteWmIssueHeaderByIssueIds(Long[] issueIds)
    {
        return deleteSQLIds(issueIds);
    }

    /**
     * 删除生产领料单头信息
     *
     * @param issueId 生产领料单头主键
     * @return 结果
     */
    @Override
    public SqlResult deleteWmIssueHeaderByIssueId(Long issueId)
    {
        return deleteSQLId(issueId);
    }

    @Override
    public List<IssueTxBean> getTxBeans(Long issueId) {
        return getBaseMapper().getTxBeans(issueId);
    }

    @Override
    public List<WmIssueHeader> getFromIds(Serializable[] ids) {
        QueryWrapper<WmIssueHeader> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("issue_id",ids);
        return list(queryWrapper);
    }
}
